/*
 * Copyright (c) 2010 Andy Aspell-Clark
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package uk.org.aspellclark.agenedb.gedcom.model;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * An individual person.
 * 
 * @author Matt
 */
public final class Individual extends GedcomRecord {

	public String ancestralFileNumber;
	public String permanentRecFileNumber;
	public String recIdNumber;
	public String regFileNumber;
	public String restrictionNotice;
	public String sex;
	public String changeDateId;
	public String addressId;
	public Address address = new Address();
	public ChangeDate changeDate = new ChangeDate();

	public List<String> aliases = new ArrayList<String>();
	public List<String> ancestorInterest = new ArrayList<String>();
	public List<Association> associations = new ArrayList<Association>();
	public List<IndividualAttribute> attributes = new ArrayList<IndividualAttribute>();
	public List<Citation> citations = new ArrayList<Citation>();
	public List<String> descendantInterest = new ArrayList<String>();
	public List<IndividualEvent> events = new ArrayList<IndividualEvent>();
	public List<FamilyChild> familiesWhereChild = new ArrayList<FamilyChild>();
	public List<FamilySpouse> familiesWhereSpouse = new ArrayList<FamilySpouse>();
	public List<LdsIndividualOrdinance> ldsIndividualOrdinances = new ArrayList<LdsIndividualOrdinance>();
	public List<Multimedia> multimedia = new ArrayList<Multimedia>();
	public List<PersonalName> names = new ArrayList<PersonalName>();
	public List<Note> notes = new ArrayList<Note>();
	public List<String> submitters = new ArrayList<String>();
	public List<UserReference> userReferences = new ArrayList<UserReference>();
	public List<String> phoneNumbers = new ArrayList<String>();

	/**
	 * Default Constructor.
	 */
	public Individual() {
	}

	public void addEvent(IndividualEvent anEvent) {
		this.events.add(anEvent);
	}

	public CharSequence getBirthPlace() {
		String dobStr = "unknown";
		for (IndividualEvent ie : this.events) {
			if (ie.type != null) {
				if (ie.type.equalsIgnoreCase(IndividualEventType.BIRTH.tag)) {
					if (ie.place != null) {
						dobStr = ie.place.placeName;
						break;
					}
				}
			}
		}
		return dobStr;
	}

	/**
	 * return the date of birth as a string;
	 * @return
	 */
	public String getDOB() {
		String dobStr = "unknown";
		for (IndividualEvent ie : this.events) {
			if (ie.type != null) {
				if (ie.type.equalsIgnoreCase(IndividualEventType.BIRTH.tag)) {
					if (ie.date != null) {
						dobStr = ie.date;
					}
				}
			}
		}
		return dobStr;
	}

	/**
	 * return the date of death as a string;
	 * @return
	 */
	public String getDOD() {
		String dobStr = "";
		for (IndividualEvent ie : this.events) {
			if (ie.type != null) {
				if (ie.type.equalsIgnoreCase(IndividualEventType.DEATH.tag)) {
					if (ie.date != null) {
						dobStr = ie.date;
					}
				}
			}
		}
		return dobStr;
	}

	public List<IndividualEvent> getEvents() {
		return this.events;
	}

	public String getName() {
		String meStr = "";
		PersonalName psn = null;
		if (this.names.size() > 0) {
			psn = this.names.get(0);
		}
		if (psn != null) {
			if (psn.basic != null) {
				final StringTokenizer tok = new StringTokenizer(psn.basic);
				// final String[] tokenList = new String[tok.countTokens()];
				// int x = 0;
				while (tok.hasMoreTokens()) {
					meStr += " " + tok.nextToken();
				}
			} else {
				meStr += (psn.givenName != null ? psn.givenName : "");
				meStr += " " + (psn.surname != null ? psn.surname : "");
			}
		}
		return meStr.trim();
	}

	public String getNameFirst() {
		String meStr = this.getName();
		if (meStr.length() > 0) {
			final StringTokenizer tok = new StringTokenizer(meStr, "/");
			meStr = tok.nextToken();
		}
		return meStr.trim();
	}

	public String getNameLast() {
		String meStr = this.getName();
		String lastname = "";
		if (meStr.length() > 0) {
			final StringTokenizer tok = new StringTokenizer(meStr, "/");
			if (tok.countTokens() > 1) {
				meStr = tok.nextToken();
				lastname = tok.nextToken();
			}
		}
		return lastname.trim();
	}

	@Override
	public String toString() {
		String meStr = this.getName();
		return meStr;
	}
}
